#ifndef FPLAYER_THREAD_H
#define FPLAYER_THREAD_H

#include <thread>

#include "CommonDefine.h"

/**
 * author: x-fan
 * date: 2024/1/10
 * desc:封装thread
 */
class Thread : public IBasic{
private:
  //线程对象
  std::thread *_pThread = nullptr;

  //线程是否还在运行
  bool _Running = false;

protected:
  /**
   * 线程的启动函数
   * @param pPtr 当前对象
   */
  virtual void running(void *pPtr) = 0;

public:
  Thread() = default;

  virtual ~Thread() {
    if (_pThread) {
      if (_pThread->joinable()) {
        _pThread->detach();
      }
      DELETE_PTR(_pThread)
    }
  }

  /**
   * 启动线程
   * @return true:线程启动  false:失败
   */
  bool startThread();

  /**
   * 停止线程
   */
  virtual void stopThread();

  /**
   * 线程是否存活
   * @return true:存活  false:反之
   */
  bool isAlive() const;

  /**
   * 等待线程的结束
   */
  void waitFinish();
};

#endif //FPLAYER_THREAD_H
